
import UIKit
import QMUIKit
import JXSegmentedView

class MYGCaseFileCellHeaderView: UIView {
    var defautIndex:Int = 0
    var slectDate:((Int)->())?
    var searchBlock:(()->())?
    var titleStr:String!
    var titles:[String]?{
        didSet{
            iniUI()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    @objc func searchAction(){
        searchBlock?()
    }
    private func iniUI(){
        addSubview(redView)
        addSubview(timeButton)
        addSubview(searchButton)
        timeButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(5)
            make.left.equalTo(29)
        }
        redView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.width.height.equalTo(7)
            make.centerY.equalTo(timeButton)
        }
        searchButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(timeButton)
        }
        
        addSubview(segmentView)
        segmentView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(timeButton.snp.bottom).offset(15)
            make.right.equalToSuperview()
            make.height.equalTo(35)
        }
    }
    private lazy var dataImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "file_n")
        return imgV
    }()
    private lazy var selectDateView: UIView = {
        let bgView = UIView()
        return bgView
    }()
    
    private lazy var redView: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "file_red")
        return imgV
    }()
    
    lazy var searchButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "file_search"), for: .normal)
        btn.addTarget(self, action: #selector(searchAction), for: .touchUpInside)
        return btn
    }()

    private lazy var timeButton: QMUIButton = {
        let btn = QMUIButton()
//        btn.setImage(UIImage(named: "Community/dropdown"), for: .normal)
//        btn.spacingBetweenImageAndTitle = 4
//        btn.imagePosition = .right
        btn.setTitle(titleStr, for: .normal)
        btn.setTitleColor(.rgba(51, 51, 51, 1), for: .normal)
        btn.titleLabel?.font = .medium(17)
        return btn
    }()
    private lazy var segmentView: JXSegmentedView = {
        let segment = JXSegmentedView()
        segment.delegate = self
        segment.dataSource = dataSource
        segment.backgroundColor = .white
        segment.defaultSelectedIndex = defautIndex
        return segment
    }()
    
    private lazy var dataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = titles!
        dataSource.titleNormalFont = .regular(14)
        dataSource.titleNormalColor = .rgba(116, 117, 118, 1)
        dataSource.titleSelectedFont = .medium(14)
        dataSource.titleSelectedColor = .rgba(143, 134, 255, 1)
        dataSource.isItemSpacingAverageEnabled = false
        return dataSource
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension MYGCaseFileCellHeaderView:JXSegmentedViewDelegate{
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int){
        slectDate?(index)
    }
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {}
    // 滚动选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {}
    // 正在滚动中的回调
    func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {}
    
}
